home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / UUCICO / NBSTIME.C < prev    next >
C/C++ Source or Header  |  1993-09-25  |  13KB  |  390 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n b s t i m e . c                                               */
  3. /*                                                                    */
  4. /*    Set local system clock from National Bureau of Standards        */
  5. /*    Standard Time service                                           */
  6. /*--------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------*/
  9. /*    Copyright (c) 1990-1993 by Kendra Electronic Wonderworks.       */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: nbstime.c 1.10 1993/09/26 03:32:27 dmwatt Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: nbstime.c $
  24.  * Revision 1.10  1993/09/26  03:32:27  dmwatt
  25.  * Use Standard Windows NT error message module
  26.  *
  27.  * Revision 1.9  1993/09/20  04:46:34  ahd
  28.  * OS/2 2.x support (BC++ 1.0 support)
  29.  * TCP/IP support from Dave Watt
  30.  * 't' protocol support
  31.  *
  32.  * Revision 1.8  1993/07/22  23:22:27  ahd
  33.  * First pass at changes for Robert Denny's Windows 3.1 support
  34.  *
  35.  * Revision 1.7  1993/06/13  14:02:32  dmwatt
  36.  * Additional Windows/NT fixes
  37.  *
  38.  * Revision 1.6  1993/05/30  00:04:53  ahd
  39.  * Multiple communications drivers support
  40.  *
  41.  * Revision 1.5  1993/04/15  04:15:43  ahd
  42.  * Twiddle OS/2 support and reduce number of #ifdef segments
  43.  *
  44.  * Revision 1.4  1993/04/13  02:27:31  dmwatt
  45.  * Windows/NT updates
  46.  *
  47.  * Revision 1.3  1993/04/11  00:34:11  ahd
  48.  * Global edits for year, TEXT, etc.
  49.  *
  50.  */
  51.  
  52. /*--------------------------------------------------------------------*/
  53. /*                        System include files                        */
  54. /*--------------------------------------------------------------------*/
  55.  
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <time.h>
  59. #include <string.h>
  60.  
  61.  
  62. #ifdef WIN32
  63.  
  64. #include <windows.h>
  65. #define NONDOS
  66.  
  67. #elif defined(FAMILYAPI) || defined(__OS2__)
  68.  
  69. #define NONDOS
  70. #define INCL_BASE
  71.  
  72. #include <os2.h>
  73.  
  74. #elif !defined(__TURBOC__)
  75.  
  76. #include <dos.h>
  77.  
  78. #endif /* WIN32 */
  79.  
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*                    UUPC/extended include files                     */
  83. /*--------------------------------------------------------------------*/
  84.  
  85. #include "lib.h"
  86. #include "arpadate.h"
  87. #include "dcp.h"
  88. #include "dcpsys.h"
  89. #include "hostable.h"
  90. #include "nbstime.h"
  91. #include "script.h"
  92. #include "security.h"
  93. #include "commlib.h"
  94.  
  95. #if !defined(__TURBOC__) || defined(__OS2__)
  96.    currentfile();
  97. #endif
  98.  
  99. #if defined(WIN32)
  100. #include "pnterr.h"
  101. #endif
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*    n b s t i m e                                                   */
  105. /*                                                                    */
  106. /*    Set system clock from using time from NBS of the format:        */
  107. /*                                                                    */
  108. /*                  MJD  YR MO DA  H  M  S ST S UT1 msADV         OTM */
  109. /*  nbs format-->  47511 88-12-16 06:03:44 00 0 -.1 045.0 UTC(NIST) * */
  110. /*  @ 1200 baud    47511 88-12-16 06:03:45 00 0 -.1 045.0 UTC(NIST) * */
  111. /*--------------------------------------------------------------------*/
  112.  
  113. boolean nbstime( void )
  114. {
  115.    char buf[BUFSIZ];
  116.    time_t today;
  117.    struct tm  tx;
  118.    int cycles = 15;
  119.    int dst= 0;
  120.    time_t delta;
  121.    char sync = '?';
  122.  
  123. #ifdef WIN32
  124.  
  125.    SYSTEMTIME DateTime;
  126.    TOKEN_PRIVILEGES tkp;
  127.    HANDLE hToken;
  128.    USHORT rc;
  129.    DWORD dwError;
  130.  
  131. #elif defined(FAMILYAPI) || defined(__OS2__)
  132.  
  133.    DATETIME DateTime;
  134.    struct tm *tp;
  135.    USHORT rc;
  136.  
  137. #elif !defined(NONDOS) && !defined(__TURBOC__)
  138.  
  139.    unsigned short rc;
  140.    struct dosdate_t ddate;
  141.    struct dostime_t dtime;
  142.    struct tm *tp;
  143.  
  144. #endif
  145.  
  146.    memset( &tx , '\0', sizeof tx);        /* Clear pointers          */
  147.    if (!expectstr("MJD", 5, NULL )) /* Margaret Jane Derbyshire? :-) */
  148.    {
  149.       printmsg(0,"nbstime: Did not find MJD literal in data from remote");
  150.       return FALSE;
  151.    }
  152.  
  153.    rmsg(buf, 2, 2, sizeof buf);
  154.                   /* Read one line to get us setup for input   */
  155.  
  156. /*--------------------------------------------------------------------*/
  157. /*                  Begin main loop to get the time                   */
  158. /*--------------------------------------------------------------------*/
  159.  
  160.    while ((rmsg(buf, 2, 2, sizeof buf) != TIMEOUT) && cycles--)
  161.    {
  162.       sync = buf[ strlen( buf ) - 1 ];
  163.  
  164.       if (sync == '#')
  165.          break;
  166.       else if (sync != '*')
  167.          *buf = '\0';
  168.  
  169.    } /* while */
  170.  
  171.    if ( (cycles && (sync == '*')) || (*buf == '\0'))
  172.    {
  173.       printmsg(0,"nbstime: Did not get good buffer: \"%s\"", buf );
  174.                   return FALSE;
  175.    }
  176.  
  177. /*--------------------------------------------------------------------*/
  178. /*                   Determine the time we received                   */
  179. /*--------------------------------------------------------------------*/
  180.  
  181.    sscanf(buf,"%*s %d-%d-%d %d:%d:%d %d ",
  182.          &tx.tm_year, &tx.tm_mon, &tx.tm_mday ,
  183.          &tx.tm_hour, &tx.tm_min, &tx.tm_sec, &dst);
  184.    tx.tm_mon--;               /* Tm record counts months from zero   */
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*     mktime()'s "renormalizing" the tm struct is screwing up NT    */
  188. /*--------------------------------------------------------------------*/
  189. #if !defined(WIN32)
  190.    today = mktime(&tx);       /* Current UTC (GMT) time in seconds   */
  191.  
  192.    if ( debuglevel > 2 )
  193.    {
  194.       printmsg(3,"%2d/%2d/%2d %2d:%2d:%2d %2d %c translates to %ld or %s",
  195.          tx.tm_year, tx.tm_mon + 1 , tx.tm_mday ,
  196.          tx.tm_hour, tx.tm_min, tx.tm_sec, dst, sync ,
  197.          today, ctime( &today ));
  198.    }
  199.  
  200. /*--------------------------------------------------------------------*/
  201. /*    Perform a sanity check; the time must be 20 years past 1970     */
  202. /*--------------------------------------------------------------------*/
  203.  
  204.    if ( today < 630720000L )
  205.    {
  206.       printmsg(0,"nbstime: Time warp error (%s), clock not set",
  207.             ctime( &today ));
  208.       return FALSE;
  209.    }
  210. #endif
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*                        Set the system clock                        */
  214. /*--------------------------------------------------------------------*/
  215.  
  216. #if defined(FAMILYAPI) || defined(__OS2__)
  217.  
  218.    rc = DosGetDateTime( &DateTime );
  219.    if ( rc != 0 )
  220.    {
  221.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  222.       panic();
  223.    }
  224.  
  225.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  226.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  227.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  228.       (int) DateTime.timezone, (int) DateTime.weekday );
  229.  
  230.    today -= timezone;
  231.    tp = localtime(&today);    /* Get local time as a record          */
  232.  
  233.    DateTime.year    = (USHORT) tp->tm_year + 1900;
  234.    DateTime.month   = (UCHAR) (tp->tm_mon + 1);
  235.    DateTime.day     = (UCHAR) tp->tm_mday;
  236.    DateTime.hours   = (UCHAR) tp->tm_hour;
  237.    DateTime.minutes = (UCHAR) tp->tm_min;
  238.    DateTime.seconds = (UCHAR) tp->tm_sec;
  239.  
  240.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  241.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  242.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  243.       (int) DateTime.timezone, (int) DateTime.weekday );
  244.  
  245.    rc = DosSetDateTime( &DateTime );
  246.    if ( rc != 0 )
  247.    {
  248.       printmsg(0,"Return c